﻿using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Sumen.Library.Constant;
using Sumen.Library.Enum;
using Sumen.Library.Sockets;
using Sumen.Business.Presenter.Sys;
using Sumen.Model.Model;

namespace Sumen.Business
{
    public class ClsPublic
    {
        static sumenEntitiesSystem _sumenEntitiesSystem = new sumenEntitiesSystem();

        #region Server socket

        public static ServerSocket serverSocket = new ServerSocket(7, 5);
        public static void StartSocket()
        {
            serverSocket.StartServer();
        }

        #endregion

        #region Employee login

        static user _user = new user();

        public static user User
        {
            get { return _user; }
            set { _user = value; }
        }

        #endregion


        #region  Parameter
        static SysParameterPresenter _parameterPresenter = new SysParameterPresenter(new sumenEntitiesSystem());

        static string getParameterById(string pId)
        {
            return _parameterPresenter.GetScalar(pId);
        }

        public static void GetListParameters()
        {
            COMPANY_ADDRESS = getParameterById(ParameterConstant.COMPANY_ADDRESS);
            COMPANY_NAME = getParameterById(ParameterConstant.COMPANY_NAME);
            COMPANY_PHONE = getParameterById(ParameterConstant.COMPANY_PHONE);
            COMPANY_CONTACT = getParameterById(ParameterConstant.COMPANY_CONTACT);
            COMPANY_FAX = getParameterById(ParameterConstant.COMPANY_FAX);
            COMPANY_WEBSITE = getParameterById(ParameterConstant.COMPANY_WEBSITE);
            COMPANY_EMAIL = getParameterById(ParameterConstant.COMPANY_EMAIL);
            COMPANY_TELEPHONE = getParameterById(ParameterConstant.COMPANY_TELEPHONE);
            /* Format information-------------------------------------------------------------------------------------------------- */
            SYSTEM_FONT_SIZE = getParameterById(ParameterConstant.SYSTEM_FONT_SIZE);
            SYSTEM_FONT_NAME = getParameterById(ParameterConstant.SYSTEM_FONT_NAME);
            SYSTEM_CULTURE = getParameterById(ParameterConstant.SYSTEM_CULTURE);
            SYSTEM_FORMAT_DATE = getParameterById(ParameterConstant.SYSTEM_FORMAT_DATE);
            /* System information-------------------------------------------------------------------------------------------------- */
            SYSTEM_SECRET_CODE = getParameterById(SYSTEM_SECRET_CODE);
            SYSTEM_WRITELOG = getParameterById(ParameterConstant.SYSTEM_WRITELOG);
            SYSTEM_DISPLAY_ACTIVED = getParameterById(ParameterConstant.SYSTEM_DISPLAY_ACTIVED);
            /* Order information-------------------------------------------------------------------------------------------------- */
            ORDERS_PREFIX_CODE = getParameterById(ParameterConstant.ORDERS_PREFIX_CODE);
            ORDERS_DATE_EXPIRED_CHANGE = getParameterById(ParameterConstant.ORDERS_DATE_EXPIRED_CHANGE);
            ORDERS_DATE_EXPIRED_RETURN = getParameterById(ParameterConstant.ORDERS_DATE_EXPIRED_RETURN);
            /* Product information-------------------------------------------------------------------------------------------------- */
            PRODUCT_CODE_PATTERN = getParameterById(ParameterConstant.PRODUCT_CODE_PATTERN);
            PRODUCT_DATE_EXPIRED_DELETED = getParameterById(ParameterConstant.PRODUCT_DATE_EXPIRED_DELETED);
            PRODUCT_SHIP = getParameterById(ParameterConstant.PRODUCT_SHIP);

            // ipWan = GetComputer_InternetIP();



            /* Load System information-------------------------------------------------------------------------------------------------- */
            float fontSize = 8.25f;
            try
            {
                fontSize = Convert.ToSingle(SYSTEM_FONT_SIZE);
            }
            catch (Exception)
            {
                fontSize = 8.25f;
            }
            
            //Thread.CurrentThread.CurrentCulture = new CultureInfo(SYSTEM_CULTURE);
            //Thread.CurrentThread.CurrentUICulture = new CultureInfo(SYSTEM_CULTURE);
            //DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font(SYSTEM_FONT_NAME, fontSize);
        }



        static string GetComputer_InternetIP()
        {
            try
            {
                // check IP using DynDNS's service
                WebRequest request = WebRequest.Create("http://checkip.dyndns.org");
                WebResponse response = request.GetResponse();
                StreamReader stream = new StreamReader(response.GetResponseStream());

                // IMPORTANT: set Proxy to null, to drastically INCREASE the speed of request
                // request.Proxy = null;

                // read complete response
                string ipAddress = stream.ReadToEnd();

                // replace everything and keep only IP
                return ipAddress.
                    Replace("<html><head><title>Current IP Check</title></head><body>Current IP Address: ", string.Empty).
                    Replace("</body></html>\r\n", string.Empty);
            }
            catch
            {
                return "Không xác định";
            }

        }


        #endregion


        #region Paramter Value

        /* Company information-------------------------------------------------------------------------------------------------- ---- */
        public static string COMPANY_ADDRESS { get; private set; }
        public static string COMPANY_NAME { get; private set; }
        public static string COMPANY_PHONE { get; private set; }
        public static string COMPANY_CONTACT { get; private set; }
        public static string COMPANY_FAX { get; private set; }
        public static string COMPANY_WEBSITE { get; private set; }
        public static string COMPANY_EMAIL { get; private set; }
        public static string COMPANY_TELEPHONE { get; private set; }


        /* Format information-------------------------------------------------------------------------------------------------- */
        public static string SYSTEM_FONT_SIZE  { get; private set; }
        public static string SYSTEM_FONT_NAME  { get; private set; }
        public static string SYSTEM_CULTURE { get; private set; }
        public static string SYSTEM_FORMAT_DATE { get; private set; }
        

        /* System information-------------------------------------------------------------------------------------------------- */
        public static string SYSTEM_SECRET_CODE { get; private set; }
        public static string SYSTEM_WRITELOG { get; private set; }
        public static string SYSTEM_DISPLAY_ACTIVED { get; private set; }



        /* Order information-------------------------------------------------------------------------------------------------- */
        public static string ORDERS_PREFIX_CODE { get; private set; }
        public static string ORDERS_DATE_EXPIRED_CHANGE { get; private set; }
        public static string ORDERS_DATE_EXPIRED_RETURN { get; private set; }




        /* Product information-------------------------------------------------------------------------------------------------- */
        public static string PRODUCT_CODE_PATTERN { get; private set; }
        public static string PRODUCT_DATE_EXPIRED_DELETED { get; private set; }
        public static string PRODUCT_SHIP { get; private set; }

        #endregion


        #region Encrypt

        public static string EncryptSHA512(string pStr)
        {
            try
            {
                byte[] inputBytes = ASCIIEncoding.ASCII.GetBytes(pStr.Trim());
                SHA512Managed hasSha512Managed = new SHA512Managed();
                //SHA256Managed hash = new SHA256Managed();
                byte[] hashBytes = hasSha512Managed.ComputeHash(inputBytes);
                string sEncrypt = Convert.ToBase64String(hashBytes);
                return sEncrypt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        #region Write Error

        public static int WriteException(Exception ex)
        {
            using (sumenEntitiesSystem obj = new sumenEntitiesSystem())
            {
                sys_error newError = new sys_error()
                {
                    Error_Detail = ex.ToString(),
                    Error_Date = DateTime.Now,
                    Error_Text = ex.Message
                };
                obj.sys_error.Add(newError);
                return obj.SaveChanges();
            }

        }

        #endregion

        #region Write log

        public static void WriteLog(EnumFormCode pFromCode, EnumFormStatus pEnumFormStatus, string pRefId, string pDesc)
        {
            if (SYSTEM_WRITELOG == "1")
            {
                SysLogPresenter log = new SysLogPresenter();
                log.Add(pFromCode, pEnumFormStatus, pRefId, pDesc);
            }
        }

        #endregion


    }
}
